Skip to content

Conversation

@YunKuiLu
Copy link
Contributor

@YunKuiLu YunKuiLu commented Jul 26, 2025

  • When toolFunction throws a ToolExecutionException, rethrow it directly.
  • When toolFunction throws a RuntimeException, wrap it in a ToolExecutionException and rethrow it.
  • Add related tests

Closes #2857

By default, FunctionToolCallback should behave similarly to MethodToolCallback.
Specifically, just like MethodToolCallback wraps method exceptions in ToolExecutionException, FunctionToolCallback should do the same.
Here’s the method from MethodToolCallback:

@Nullable
private Object callMethod(Object[] methodArguments) {
if (isObjectNotPublic() || isMethodNotPublic()) {
this.toolMethod.setAccessible(true);
}
Object result;
try {
result = this.toolMethod.invoke(this.toolObject, methodArguments);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException("Could not access method: " + ex.getMessage(), ex);
}
catch (InvocationTargetException ex) {
throw new ToolExecutionException(this.toolDefinition, ex.getCause());
}
return result;
}

- When `toolFunction` throws a `ToolExecutionException`, rethrow it directly.
- When `toolFunction` throws a `Exception`, wrap it in a `ToolExecutionException` and rethrow it.
- Add related tests

Signed-off-by: YunKui Lu <[email protected]>
@YunKuiLu YunKuiLu force-pushed the function_tool_err branch from c6dd6b8 to b4ad3ed Compare July 26, 2025 08:19
@sobychacko sobychacko merged commit 1b5cd09 into spring-projects:main Aug 6, 2025
2 checks passed
spring-builds pushed a commit that referenced this pull request Aug 6, 2025
#3918)

Fixes #2857

- When `toolFunction` throws a `ToolExecutionException`, rethrow it directly.
- When `toolFunction` throws a `Exception`, wrap it in a `ToolExecutionException` and rethrow it.
- Add related tests

Signed-off-by: YunKui Lu <[email protected]>
(cherry picked from commit 1b5cd09)
@YunKuiLu YunKuiLu deleted the function_tool_err branch August 8, 2025 16:25
scionaltera pushed a commit to scionaltera/spring-ai that referenced this pull request Sep 3, 2025
spring-projects#3918)

Fixes spring-projects#2857

Auto-cherry-pick to 1.0.x

- When `toolFunction` throws a `ToolExecutionException`, rethrow it directly.
- When `toolFunction` throws a `Exception`, wrap it in a `ToolExecutionException` and rethrow it.
- Add related tests

Signed-off-by: YunKui Lu <[email protected]>
chedim pushed a commit to couchbaselabs/spring-ai that referenced this pull request Sep 19, 2025
spring-projects#3918)

Fixes spring-projects#2857

Auto-cherry-pick to 1.0.x

- When `toolFunction` throws a `ToolExecutionException`, rethrow it directly.
- When `toolFunction` throws a `Exception`, wrap it in a `ToolExecutionException` and rethrow it.
- Add related tests

Signed-off-by: YunKui Lu <[email protected]>
Willam2004 pushed a commit to Willam2004/spring-ai that referenced this pull request Oct 11, 2025
spring-projects#3918)

Fixes spring-projects#2857

Auto-cherry-pick to 1.0.x

- When `toolFunction` throws a `ToolExecutionException`, rethrow it directly.
- When `toolFunction` throws a `Exception`, wrap it in a `ToolExecutionException` and rethrow it.
- Add related tests

Signed-off-by: YunKui Lu <[email protected]>
Signed-off-by: 家娃 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Error handling when calling MCP tools

2 participants